// pages/Shopping_Cart/Shopping_Cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopCar: [],
    shopNum: 0,
    total: 0,  //总金额
    //goodsCount:0  //数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  goShop: function () {
    wx.switchTab({
      url: '../index/index'
    })
  },
  onShow: function () {
    //获取购物车的缓存数组
    var arr = wx.getStorageSync("shopCar") || [];
    var Num = 0;
    var sum = 0;
    if (arr.length > 0) {
      for (var i in arr) {
        //将价格变为数字型循环加
        sum = sum + Number(arr[i].price) * arr[i].buyNumber;
        Num = Num + arr[i].buyNumber;
      }
      //数量进行合计

      //进行更新数据
      this.setData({
        shopCar: arr,
        total: sum,
        shopNum: Num
      })
    }
  },
  //减数量
  delCount: function (e) {
    //获取当前该btn的id
    if (this.data.shopCar[e.target.id.substring(3)].buyNumber <= 1) {
      return
    }
    //当前该商品数量减一
    this.data.shopNum = this.data.shopNum - 1;
    //总价格减掉商品的单价
    this.data.total = this.data.total - Number(this.data.shopCar[e.target.id.substring(3)].price);
    //对应物体的数量减一
    this.data.shopCar[e.target.id.substring(3)].buyNumber = --this.data.shopCar[e.target.id.substring(3)].buyNumber;
    //更新数据
    this.setData({
      shopCar: this.data.shopCar,
      total: this.data.total,
      shopNum: this.data.shopNum,
    })
    //缓存更新
    try {
      wx.setStorageSync("shopCar", this.data.shopCar)
    } catch (e) {
      console.log(e);
    }
  },
  //加数量
  addCount: function (e) {
    this.data.shopNum = this.data.shopNum + 1;
    //总价格加商品的单价
    this.data.total = this.data.total + Number(this.data.shopCar[e.target.id.substring(3)].price);
    //对应物体的数量加一
    this.data.shopCar[e.target.id.substring(3)].buyNumber = ++this.data.shopCar[e.target.id.substring(3)].buyNumber;
    //更新数据
    this.setData({
      shopCar: this.data.shopCar,
      total: this.data.total,
      shopNum: this.data.shopNum,
    })
    //缓存更新
    try {
      wx.setStorageSync("shopCar", this.data.shopCar)
    } catch (e) {
      console.log(e);
    }
  },
  //删除
  delGoods: function (e) {
    //删掉对应产品的数量以及价格
    this.data.shopNum = this.data.shopNum - this.data.shopCar[e.target.id.substring(3)].buyNumber;
    this.data.total = this.data.total - this.data.shopCar[e.target.id.substring(3)].price * this.data.shopCar[e.target.id.substring(3)].buyNumber;
    //数组移除该项
    this.data.shopCar.splice(e.target.id.substring(3), 1);
    //更新数据
    this.setData({
      shopCar: this.data.shopCar,
      shopNum: this.data.shopNum,
      total: this.data.total
    })
    //缓存
    try {
      wx.setStorageSync("shopCar", this.data.shopCar)
    } catch (e) {
      console.log(e);
    }
  },
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    var that = this;
    setTimeout(function () {
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      var arr = wx.getStorageSync("shopCar") || [];
      if (arr.length > 0) {
        for (var i in arr) {
          //将价格变为数字型循环加
          this.data.total = this.data.total + Number(arr[i].price);
          this.data.shopNum = this.data.shopNum + Number(arr[i].buyNumber);
        }
        //数量进行合计

        //进行更新数据
        this.setData({
          shopCar: arr,
          total: this.data.total,
          shopNum: this.data.shopNum
        })
      }
    }, 1500);
  },
  toPay: function () {
    //向页面传递订单金额并显示是从购物车中跳转
    wx.navigateTo({
      url: '../to_pay/to_pay?total=' + this.data.total + '&from=Shopping_Cart'
    })
  }
})